package com.headFirstModel.iteratorPattern;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * 煎饼屋菜单实现
 */
public class PancakeHouseMenu implements Menu{
    //通过ArrayList作为容器保存菜单列表
    ArrayList<MenuItem> menuItems;

    //创建对象时，存储菜单
    public PancakeHouseMenu() {
        menuItems=new ArrayList<>();
        addItem("蔬菜煎饼","蔬菜+煎饼而成",true,2.99);
        addItem("鸡肉煎饼","鸡肉+面粉而成",false,2.99);
        addItem("瘦肉煎饼","瘦肉+面粉而成",false,2.99);
    }

    private void addItem(String name, String description, boolean vegetarian, double price) {
        MenuItem menuItem=new MenuItem(name,description,vegetarian,price);
        menuItems.add(menuItem);
    }

    public ArrayList getMenuItems(){
        return menuItems;
    }

    //菜单的其他方法.....
    //实现迭代器
    final class PancakeHouseMenuIterator implements Iterator<MenuItem> {
        ArrayList<MenuItem> items;
        int position=0; //指针的作用

        public PancakeHouseMenuIterator(){
            //需要迭代的对象
            this.items=menuItems;
        }

        @Override
        public boolean hasNext() {
            if(position>=items.size()||items.get(position)==null){
                return  false;
            }else{
                return  true;
            }
        }

        @Override
        //返回当前对象并将指针指向下一位
        public MenuItem next() {
            MenuItem menuItem=items.get(position);
            position++;
            return  menuItem;
        }
    }

    public Iterator createIterator(){
       // return  new PancakeHouseMenuIterator();
        //使用ArrayList 的迭代器
        return  menuItems.iterator();
    }
}
